package com.cykj.pos.controller.api;

import java.math.BigDecimal;
import java.util.List;
import java.util.Arrays;

import com.alibaba.fastjson.JSONObject;
import com.cykj.common.core.domain.entity.SysUser;
import com.cykj.pos.domain.*;
import com.cykj.pos.service.IBizMerchantService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cykj.common.annotation.Log;
import com.cykj.common.core.controller.BaseController;
import com.cykj.common.core.domain.AjaxResult;
import com.cykj.common.enums.BusinessType;
import com.cykj.pos.service.ISettlementRateTemplateService;
import com.cykj.common.utils.poi.ExcelUtil;
import com.cykj.common.core.page.TableDataInfo;

/**
 * 结算模板Controller
 *
 * @author weijianbo
 * @date 2025-01-20
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/pos/settlement/template" )
public class SettlementRateTemplateController extends BaseController {

    private final ISettlementRateTemplateService iSettlementRateTemplateService;
    private final IBizMerchantService iBizMerchantService;
    /**
     * 查询结算模板列表
     */
    @PreAuthorize("@ss.hasPermi('pos:template:list')")
    @GetMapping("/list")
    public TableDataInfo list(SettlementRateTemplate settlementRateTemplate) {
        startPage();
        List<SettlementRateTemplate> list = iSettlementRateTemplateService.queryList(settlementRateTemplate);
        return getDataTable(list);
    }

    /**
     * 导出结算模板列表
     */
    @PreAuthorize("@ss.hasPermi('pos:template:export')" )
    @Log(title = "结算模板" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(SettlementRateTemplate settlementRateTemplate) {
        List<SettlementRateTemplate> list = iSettlementRateTemplateService.queryList(settlementRateTemplate);
        ExcelUtil<SettlementRateTemplate> util = new ExcelUtil<SettlementRateTemplate>(SettlementRateTemplate.class);
        return util.exportExcel(list, "template" );
    }

    /**
     * 获取结算模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('pos:template:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iSettlementRateTemplateService.getById(id));
    }

    /**
     * 新增结算模板
     */
    @PreAuthorize("@ss.hasPermi('pos:template:add')" )
    @Log(title = "结算模板" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SettlementRateTemplate settlementRateTemplate) {
        return toAjax(iSettlementRateTemplateService.save(settlementRateTemplate) ? 1 : 0);
    }

    /**
     * 修改结算模板
     */
    @PreAuthorize("@ss.hasPermi('pos:template:edit')" )
    @Log(title = "结算模板" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SettlementRateTemplate settlementRateTemplate) {
        return toAjax(iSettlementRateTemplateService.updateById(settlementRateTemplate) ? 1 : 0);
    }

    /**
     * 删除结算模板
     */
    @PreAuthorize("@ss.hasPermi('pos:template:remove')" )
    @Log(title = "结算模板" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iSettlementRateTemplateService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    // app 新增结算模板
    @PostMapping("/add/settlementRateTemplate")
    public AjaxResult addSettlementRateTemplate(@RequestBody SettlementRateTemplate settlementRateTemplate) {
        BizMerchant bizMerchant = iBizMerchantService.getMerchantByUserId(settlementRateTemplate.getUserId());
        Long merchId = bizMerchant.getMerchId();
        settlementRateTemplate.setMerchId(merchId);
        settlementRateTemplate.setCreditRate(settlementRateTemplate.getCreditRate().divide(new BigDecimal(100)));
        settlementRateTemplate.setDebitFee(settlementRateTemplate.getDebitFee().divide(new BigDecimal(100)));
        settlementRateTemplate.setQrCodeRate(settlementRateTemplate.getQrCodeRate().divide(new BigDecimal(100)));
        return toAjax(iSettlementRateTemplateService.save(settlementRateTemplate) ? 1 : 0);
    }


    // update 修改结算模板
    @PostMapping("/update/settlementRateTemplate")
    public AjaxResult updateSettlementRateTemplate(@RequestBody SettlementRateTemplate settlementRateTemplate) {
        settlementRateTemplate.setCreditRate(settlementRateTemplate.getCreditRate().divide(new BigDecimal(100)));
        settlementRateTemplate.setDebitFee(settlementRateTemplate.getDebitFee().divide(new BigDecimal(100)));
        settlementRateTemplate.setQrCodeRate(settlementRateTemplate.getQrCodeRate().divide(new BigDecimal(100)));
        return toAjax(iSettlementRateTemplateService.saveOrUpdate(settlementRateTemplate) ? 1 : 0);
    }


    // app  结算模板回显列表
    @PostMapping("/back/show/settlementRateTemplate")
    public AjaxResult backShowSettlementRateTemplate(@RequestBody SettlementRateTemplate settlementRateTemplate){
        BizMerchant bizMerchant = iBizMerchantService.getMerchantByUserId(settlementRateTemplate.getUserId());
        Long merchId = bizMerchant.getMerchId();
        List<SettlementRateTemplate> settlementRateTemplateList = iSettlementRateTemplateService.getSettlementRateTemplateByMerchId(merchId);
        return AjaxResult.success("列表",settlementRateTemplateList);
    }


    // 详情
    @PostMapping("/settlementRateTemplate/detail")
    public AjaxResult getInfo(@RequestBody SettlementRateTemplate settlementRateTemplate) {
        settlementRateTemplate =iSettlementRateTemplateService.getById(settlementRateTemplate.getId());
        settlementRateTemplate.setQrCodeRate(settlementRateTemplate.getQrCodeRate().multiply(new BigDecimal(100)).setScale(2,BigDecimal.ROUND_DOWN));
        settlementRateTemplate.setDebitFee(settlementRateTemplate.getDebitFee().multiply(new BigDecimal(100)).setScale(3,BigDecimal.ROUND_UP));
        settlementRateTemplate.setCreditRate(settlementRateTemplate.getCreditRate().multiply(new BigDecimal(100)).setScale(3,BigDecimal.ROUND_UP));
        return AjaxResult.success(settlementRateTemplate);
    }



    @ApiOperation("结算模板费率区间")
    @PostMapping("/query/interval")
    public AjaxResult queryInterval(@RequestBody SysUser sysUser){
        BizMerchant bizMerchant = iBizMerchantService.getMerchantByUserId(sysUser.getUserId());
        Long merchId = bizMerchant.getMerchId();
        JSONObject result = new JSONObject();
        //信用卡刷卡
        BigDecimal creditRateMin = new BigDecimal(0.00505);
        // 借记卡刷卡
        BigDecimal debitRateMin = new BigDecimal(0.00505);
        // 借记卡封顶
        BigDecimal debitCapMin = new BigDecimal("42");
        // 调价分成
        BigDecimal commission = new BigDecimal("100");
        // 扫码
        BigDecimal qrCodeRateMin = new BigDecimal(0.0038);
        // D0
        BigDecimal d0 = new BigDecimal("3");

        int count = iSettlementRateTemplateService.checkIsThereSnByMerchId(merchId);
        if(count != 0){
            //信用卡刷卡
            creditRateMin = iSettlementRateTemplateService.getSettlementCreditRateMinByMerchId(merchId);
            // 借记卡刷卡
            debitRateMin = iSettlementRateTemplateService.getSettlementDebitRateMinByMerchId(merchId);
            // 借记卡封顶
            debitCapMin = iSettlementRateTemplateService.getSettlementDebitCapMinByMerchId(merchId);
            // 调价分成
            commission = iSettlementRateTemplateService.getSettlementCommissionMinByMerchId(merchId);
            // 扫码
            qrCodeRateMin = iSettlementRateTemplateService.getSettlementQrCodeRateMinMinByMerchId(merchId);
            // D0
            d0 = iSettlementRateTemplateService.getSettlementD0MinByMerchId(merchId);
        }
        if(merchId == 2){
            result.put("creditRate1","0.505");
            result.put("creditRate2","0.6");
            result.put("debitRate1","0.505");
            result.put("debitRate2","0.6");
            result.put("debitCap1","0");
            result.put("debitCap2","42");
            result.put("commission1","0");
            result.put("commission2","100");
            result.put("qrCodeRate1","0.29");
            result.put("qrCodeRate2","0.38");
            result.put("d01","0");
            result.put("d02","3");
        }else{
            result.put("creditRate1",creditRateMin.multiply(new BigDecimal(100)).setScale(3,BigDecimal.ROUND_UP));
            result.put("creditRate2","0.6");
            result.put("debitRate1",debitRateMin.multiply(new BigDecimal(100)).setScale(3,BigDecimal.ROUND_UP));
            result.put("debitRate2","0.6");
            result.put("debitCap1","0");
            result.put("debitCap2", debitCapMin);
            result.put("commission1","0");
            result.put("commission2",commission);
            result.put("qrCodeRate1",qrCodeRateMin.multiply(new BigDecimal(100)).setScale(2,BigDecimal.ROUND_DOWN));
            result.put("qrCodeRate2","0.38");
            result.put("d01","0");
            result.put("d02",d0 );
        }
        return AjaxResult.success("结算政策模版",result);
    }



    // app 回显提现政策模板列表
    @PostMapping("/back/withdrawal/template/list")
    public AjaxResult backWithdrawalTemplateList(@RequestBody  WithdrawalRateTemplate withdrawalRateTemplate){
        return iSettlementRateTemplateService.getWithdrawalTemplateList(withdrawalRateTemplate);
    }


    // app 回显提现政策模板明细
    @PostMapping("/back/withdrawal/template/detail")
    public AjaxResult backWithdrawalTemplateDeatil(@RequestBody WithdrawalRateTemplate withdrawalRateTemplate){
        return iSettlementRateTemplateService.backWithdrawalTemplateDeatil(withdrawalRateTemplate);
    }


    // app 回显返现模板列表
    @PostMapping("/back/toolCashback/template/List")
    public AjaxResult backToolCashbackTemplateList(){
        return iSettlementRateTemplateService.backToolCashbackTemplateList();
    }


    // app 回显返现模板明细
    @PostMapping("/back/toolCashback/template/detail")
    public AjaxResult backToolCashbackTemplateDetail(@RequestBody ToolCashbackTemplate toolCashbackTemplate){
        return iSettlementRateTemplateService.backToolCashbackTemplateDetail(toolCashbackTemplate);
    }
}
